/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ticketmaster.dao;

import ticketmaster.dataobject.Seat;
import javax.sql.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
/**
 *
 * @author Xiaolong Xue
 */
public class SeatDAO extends BaseEntityDAO
{
    public SeatDAO()
    {
        //construct a SeatDAO object
    }

    /**
     * Insert a new Seat object into database
     * @param seat the new seat entity to be inserted
     * @return newly created seat ID, or  0 when error
     */
    public int insertSeat(Seat seat)
    {
        return save(seat);
    }

    /**
     * Delete a Seat object that matches the supplied criteria
     * @param seat the supplied criteria
     * @return true if success, or false
     */
     public boolean deleteSeat(Seat seat)
     {
         return delete(seat);
     }

    /**
     * Update the seat by using data from the SeatCategory Data Object(from UI)
     * @param seat the seat Data Object to be updated
     * @return true on success, false on failure or error
     */
     public boolean update(Seat seat)
     {
         if(seat.getVenueID() == 0 || seat.getSeatCategoryID() == 0)
             return false;
         if(update(seat))
             return true;
         else
             return false;
     }

     /**
     * Get the seat that matches the supplied criteria
     * @param seat the supplied criteria
     * @return a Seat Object if found, or null on error or if not found
     */
     public Seat findSeat(Seat seat)
     {
         String sql = null;
         if(seat.getVenueID() == 0 || seat.getSeatCategoryID() == 0)
             return null;
         else
             sql = buildQuerySQL(seat);

         RowSet srs = query(sql);
         try
         {
             srs.next();
         }
         catch(SQLException ex)
         {
             Logger.getLogger(SeatDAO.class.getName()).log(Level.SEVERE, null, ex);
         }

         return (Seat) this.getObject(srs, seat);
     }

    /**
     * Get all seat that match the supplied criteria
     * @param seat supplied criteria
     * @return a RowSet of Seat
     */
     public RowSet getSeatRowSet(Seat seat)
     {
         String sql = buildQuerySQL(seat);
         return query(sql);
     }

    /**
     * Get all seat that match the supplied criteria
     * @param seat supplied criteria
     * @return a collection of Seat Data Objects
     */
     public Collection<Seat> getSeatCollection(Seat seat)
     {
         String sql = buildQuerySQL(seat);
         RowSet srs = query(sql);
         Collection<Seat> seats = new ArrayList<Seat>();
         try
         {
             while(srs.next())
             {
                 Seat seatDO = (Seat) this.getObject(srs, seat);
                 Seat seatItem = new Seat();
                 seatItem.setSeatCategoryID(seatDO.getSeatCategoryID());
                 seatItem.setNumberOfSeats(seatDO.getNumberOfSeats());
                 seatItem.setVenueID(seatDO.getVenueID());
                 
                 seats.add(seatItem);
             }
         }
         catch(SQLException ex)
         {
             Logger.getLogger(SeatDAO.class.getName()).log(Level.SEVERE, null, ex);
         }

         return seats;
     }
}
